package com.central.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.central.common.constant.CommonConstant;
import com.central.common.model.LoginAppUser;
import com.central.common.model.SuperEntity;
import com.central.common.model.SysMenu;
import com.central.common.model.SysRole;
import com.central.common.model.SysUserAccount;
import com.central.common.service.impl.SuperServiceImpl;
import com.central.user.mapper.SysRoleMenuMapper;
import com.central.user.mapper.SysUserAccountMapper;
import com.central.user.service.ISysRoleUserService;
import com.central.user.service.ISysUserAccountService;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * @author: ly
 * @date: 2020/11/17 5:08 下午
 * @email: 664162337@qq.com
 */
@Slf4j
@Service
public class SysUserAccountServiceImpl extends SuperServiceImpl<SysUserAccountMapper, SysUserAccount> implements
        ISysUserAccountService {

    @Resource
    private ISysRoleUserService roleUserService;

    @Resource
    private SysRoleMenuMapper roleMenuMapper;

    @Override
    public LoginAppUser findByUsername(String username) {
        SysUserAccount account = selectByUsername(username);
        return getLoginAppUser(account);
    }

    @Override
    public LoginAppUser findByUserId(String userId) {
        SysUserAccount account = selectByUserId(userId);
        return getLoginAppUser(account);
    }

    @Override
    public boolean isSupperAdminUser(String username) {
        /*
        通过username得到account，account得到user，user关联到role，是否是超级管理
         */
        SysUserAccount account = selectByUsername(username);
        if (account != null) {
            List<SysRole> sysRoles = roleUserService.findRolesByUserId(Long.parseLong(account.getUserId()));
            if (!sysRoles.isEmpty()) {
                for (SysRole sysRole : sysRoles) {
                    if (sysRole.getCode().equals(CommonConstant.ADMIN_USER_NAME)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    private LoginAppUser getLoginAppUser(SysUserAccount account) {
        if (account != null) {
            LoginAppUser loginAppUser = new LoginAppUser();
            BeanUtils.copyProperties(account, loginAppUser);
            List<SysRole> sysRoles = roleUserService.findRolesByUserId(Long.parseLong(account.getUserId()));
            // 设置角色
            loginAppUser.setRoles(sysRoles);

            if (!CollectionUtils.isEmpty(sysRoles)) {
                Set<Long> roleIds = sysRoles.parallelStream().map(SuperEntity::getId).collect(Collectors.toSet());
                List<SysMenu> menus = roleMenuMapper.findMenusByRoleIds(roleIds, CommonConstant.PERMISSION);
                if (!CollectionUtils.isEmpty(menus)) {
                    Set<String> permissions = menus.parallelStream().map(SysMenu::getPath)
                            .collect(Collectors.toSet());
                    // 设置权限集合
                    loginAppUser.setPermissions(permissions);
                }
            }
            return loginAppUser;
        }
        return null;
    }

    private SysUserAccount selectByUsername(String username) {
        List<SysUserAccount> users = baseMapper.selectList(
                new QueryWrapper<SysUserAccount>().eq("username", username)
        );
        return getUser(users);
    }

    private SysUserAccount getUser(List<SysUserAccount> users) {
        SysUserAccount user = null;
        if (users != null && !users.isEmpty()) {
            user = users.get(0);
        }
        return user;
    }

    private SysUserAccount selectByUserId(String userId) {
        List<SysUserAccount> users = baseMapper.selectList(
                new QueryWrapper<SysUserAccount>().eq("user_id", userId)
        );
        return getUser(users);
    }
}
